name: "Output the container repository and image tag to be used"
description: "Container repository is inputs.registry/inputs.container-image.
  Image tag is latest if branch is main, otherwise it is the tag or branch name with slashes replaced by hyphens."

inputs:
  registry:
    description: "Registry where to push container image"
    required: true
  container-image:
    description: "The name of the container image"
    required: true

outputs:
  container-repo:
    description: "Container repository to be used."
    value: ${{ steps.choose-repo.outputs.container-repo }}
  image-tag:
    description: "Image tag to be used."
    value: ${{ steps.craft-image-tag.outputs.image-tag }}
  gadget-repository:
    description: "OCI repository used for gadgets's OCI images."
    value: ${{ steps.gadget-repository.outputs.gadget-repository }}
  gadget-tag:
    description: "Tag used for gadgets's OCI images."
    value: ${{ steps.gadget-tag.outputs.gadget-tag }}

runs:
  using: "composite"
  steps:
    - name: Output container repository
      id: choose-repo
      shell: bash
      run: |
        if [ -z '${{ inputs.registry }}' ]; then
          echo 'inputs.registry should not be empty! Set it before calling this action' 1>&2

          exit 1
        fi

        if [ -z '${{ inputs.container-image }}' ]; then
          echo 'inputs.container-image should not be empty! Set it before calling this action' 1>&2

          exit 1
        fi

        container_repo=${{ inputs.registry }}/${{ inputs.container-image }}
        echo "container-repo=${container_repo}" >> $GITHUB_OUTPUT
    - name: Output image tag
      id: craft-image-tag
      shell: bash
      run: |
        # If GITHUB_REF_NAME is 'foo/bar', image_tag will be 'foo-bar', we need
        # this because it is not possible to have slash in image tag.
        image_tag=${GITHUB_REF_NAME//\//-}
        if [ "$image_tag" = "main" ]; then
            image_tag="latest"
        fi

        echo "image-tag=${image_tag}" >> $GITHUB_OUTPUT
    - name: Output gadgets repository
      id: gadget-repository
      shell: bash
      run: |
        if [ -z '${{ inputs.registry }}' ]; then
          echo 'inputs.registry should not be empty! Set it before calling this action' 1>&2
          exit 1
        fi

        gadget_repository=${{ inputs.registry }}/${{ github.repository_owner }}/gadget
        if [ ${{ github.event_name }} == 'pull_request' ]; then
          gadget_repository=ttl.sh/${{ github.sha }}
        fi

        echo "gadget-repository=${gadget_repository}" >> $GITHUB_OUTPUT
    - name: Output gadgets image tag
      id: gadget-tag
      shell: bash
      run: |
        # If GITHUB_REF_NAME is 'foo/bar', image_tag will be 'foo-bar', we need
        # this because it is not possible to have slash in image tag.
        gadget_tag=${GITHUB_REF_NAME//\//-}
        if [ "$gadget_tag" = "main" ]; then
            gadget_tag="latest"
        fi

        echo "gadget-tag=${gadget_tag}" >> $GITHUB_OUTPUT
